import { message } from 'antd';
import {
    getHolidaylist,
    holidayDelete,
    getTypelist
} from '@/services/attendance/holidayManagement';

export default {
    namespace: 'holidaymanagement',
    state: {
        holidayList: [],
        pagination: {
            current: 1,
            pageSize: 10,
            total: 0
        },
        leaveUnit: [], // 请假单位
        leaveTimeway: [], // 计算时长方式
        isMoney: [], // 是否带薪
        balanceRule: [], // 余额规则
        leaveUnitlist: [], // 请假单位
        leaveTimewaylist: [], // 计算时长方式
        isMoneylist: [], // 是否带薪
        balanceRulelist: [] // 余额规则
    },
    effects: {
        // 假期管理列表
        *fetchHolidayList({ payload }, { call, put }) {
            const result = yield call(getHolidaylist, payload);
            if (result && result.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        holidayList: result.data.rows,
                        pagination: {
                            pageSize: result.data.pageSize,
                            current: result.data.pageNo,
                            total: result.data.total
                        }
                    }
                });
            }
        },
        // 假期删除
        *holidayDelete({ payload, success }, { call }) {
            // alert(22);
            const result = yield call(holidayDelete, payload);
            if (result.code === 10000) {
                message.success(result.msg);
                // eslint-disable-next-line no-unused-expressions
                success && success();
            }
        },
        // 四个筛选列内容
        *fetchFilterList({ payload }, { call, put }) {
            const result = yield call(getTypelist, payload);
            const { data } = result;
            if (result && result.code === 10000) {
                const newData = [];
                data.forEach(item => {
                    newData.push({
                        value: item.dictValue,
                        text: item.dictName
                    });
                });
                if (payload.dictCode === 'leaveUnitType') {
                    yield put({
                        type: 'save',
                        payload: {
                            leaveUnitlist: newData
                        }
                    });
                }
                if (payload.dictCode === 'leaveCountType') {
                    yield put({
                        type: 'save',
                        payload: {
                            leaveTimewaylist: newData
                        }
                    });
                }
                if (payload.dictCode === 'salaryCountType') {
                    yield put({
                        type: 'save',
                        payload: {
                            isMoneylist: newData
                        }
                    });
                }
                if (payload.dictCode === 'validRuleSelectType') {
                    yield put({
                        type: 'save',
                        payload: {
                            balanceRulelist: newData
                        }
                    });
                }
            }
        }
    },
    reducers: {
        save(state, { payload }) {
            return {
                ...state,
                ...payload
            };
        }
    }
};
